<template>
	<view class="container">
		<customNavigation title=" " src="" src2="../static/images/nav_icon_logo.png" src2Left="5" color="white" background="#32A238"></customNavigation>	
		<view class="main">
			<image class="Logo" src="../../static/images/login_logo.png"></image>
			<view class="item username">
				<image src="../../static/images/login_icon_yhm.png" mode=""></image>
				<input type="text" value="" placeholder="请输入用户名" placeholder-class="inputClass" v-model="username"/>
			</view>
			<view class="item password">
				<image src="../../static/images/login_icon_mm.png" mode=""></image>
				<input type="password" value="" placeholder="请输入密码" placeholder-class="inputClass" v-model="password"/>
			</view>
			
			<button :class="['loginBtn',{'canSub':(username&&password)}]" @click="subLogin()">
				立即登录
			</button>
		</view>
	</view>
</template>

<script>
	import customNavigation from "@/components/custom-navigation.vue"
	import * as api from "../../utils/controller.js"
	import md5 from "../../utils/md5.js"
	export default {
		components:{
			customNavigation
		},
		data() {
			return {
				username:"T02E001",
				password:"T02E001",
				appId:"wx27129e56f6eed972",
				secret:"fee75a2bcf80d42f131c158d3a37aac4",
				appCode:"pyzl_anbiao",
				appsecret:"5EB02B7F96F24E4BA7082BE11F5FD567",
				openid:""
			};
		},
		onLoad() {
			this.getOpenId()
		},
		methods:{
			subLogin(){
				if(!this.username){
					return
				}
				if(!this.password){
					return
				}
				let that=this
				uni.showLoading({
					title:"登录中"
				})
				uni.login({
				  provider: 'weixin',
				  success: function (res) {
				  uni.login({
					success:function(res){
						api.getOpenId({json_code:res.code}).then(res=>{
							that.openId=JSON.parse(res.data).openid
							let value={}
							value.username=that.username
							value.password=md5(that.password)
							value.appCode=that.appCode
							value.appsecret=that.appsecret
							value.openid=that.openId
							that.Login(value)
						})
					}
				  })
				   // uni.login({
				   // 			provider: 'weixin',
				   // 			success: function(res) {
				   // 				let appid = that.appId
				   // 				let secret = that.secret
				   // 				let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' +
				   // 							res.code + '&grant_type=authorization_code';
				   // 				uni.request({
				   // 					url: url, // 请求路径
				   // 					success: result => {
				   // 							console.info(result.data.openid);
							// 				let value={}
							// 				value.username=that.username
							// 				value.password=md5(that.password)
							// 				value.appCode=that.appCode
							// 				value.appsecret=that.appsecret
							// 				value.openid=result.data.openid
							// 				that.Login(value)
				   // 						},
				   // 					});
				   // 				}
				   // });
				  }
				});
				
				// let value={}
				// value.username=this.username
				// value.password=this.password
				// value.appcode="pyzl_anbiao"
				// value.appsecret="5EB02B7F96F24E4BA7082BE11F5FD567"
				// api.post({}).then(res=>{
					
				// })
			},
			
			Login(value){
				api.login(value).then(res=>{
					uni.hideLoading()
					uni.setStorageSync("token",res.data.token)
					uni.setStorageSync("userMsg",res.data)
					let userList=res.data.type.split(",")
					uni.setStorageSync("userList",userList)
					uni.reLaunch({
						url:"/pages/index/index"
					})
				})
			},
			
			//自动登录
			automaticLogon(){
				let value={}
				value.appCode=this.appCode
				value.appsecret=this.appsecret
				value.openid=this.openId
				api.automaticLogon(value).then(res=>{
					uni.hideLoading()
					uni.setStorageSync("token",res.data.token)
					uni.setStorageSync("userMsg",res.data)
					let userList=res.data.type.split(",")
					uni.setStorageSync("userList",userList)
					uni.reLaunch({
						url:"/pages/index/index"
					})
				})
			},
			
			//获取openId
			getOpenId(){
				let that=this
				uni.showLoading({
					title:"登录中"
				})
				uni.login({
					success:function(res){
						api.getOpenId({json_code:res.code}).then(res=>{
							that.openId=JSON.parse(res.data).openid
							that.automaticLogon()
						})
					}
				})
			}
		}
	}
</script>

<style lang="less">
.main{
	width: 100%;
	height: auto;
	display: flex;
	flex-direction: column;
	align-items: center;
	margin-top: 11vh;
	.Logo{
		width: 422rpx;
		height: 178rpx;
		margin-top: 120rpx;
		margin-bottom: 120rpx;
	}
	.item{
		width: 601rpx;
		height: 99rpx;
		border: 2rpx solid #EBEBEB;
		border-radius: 50rpx;
		display: flex;
		align-items: center;
		margin-bottom: 32rpx;
		image{
			width: 48rpx;
			height: 48rpx;
			margin-right: 13rpx;
			margin-left: 29rpx;
		}
		input{
			height: 100%;
			width: 420rpx;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #333333;
		}
		.inputClass{
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #999999;
		}
	}
	.loginBtn{
		margin-top: 100rpx;
		width: 600rpx;
		height: 90rpx;
		background: #32A238;
		opacity: 0.5;
		border-radius: 45rpx;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 90rpx;
	}
	.canSub{
		opacity: 1;
	}
}
</style>
